package com.taikang.hmp.hservice.doctor.pm25.action.intface;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.ErrorConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hservice.doctor.pm25.service.IWeatherInfoService;

public class AirNowAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(AirNowAction.class);
	private IWeatherInfoService weatherInfoService;
	
	public IWeatherInfoService getWeatherInfoService() {
		return weatherInfoService;
	}

	public void setWeatherInfoService(IWeatherInfoService weatherInfoService) {
		this.weatherInfoService = weatherInfoService;
	}

	public String executeAction() {
		if ("getAirNowByCityName".equals(requestDto.getMethod())) { 
			logger.info("--【通过城市名称获取pm2.5】: method getAirNowByCityName --");
			return getAirNowByCityName();
		}else {
			logger.info("executeAction 里没有配置"+ requestDto.getMethod() +"方法!");
			throw new TKException("no support method:" + requestDto.getMethod());
		}
	}

	/**
	 * 通过城市名称获取pm2.5
	 * @return
	 */
	public String getAirNowByCityName() {
		ResponseDto responseDto = new ResponseDto();
		Dto resultDto = new BaseDto();
		JSONObject airNowObject=null;
		try {
			String cityName= requestDto.getRequestlist().get(0).get("cityname").toString();
			//前台传来的进行解码
			String cityName1="";
			try {
				cityName1 = URLDecoder.decode(cityName,"UTF-8");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			//心知天气已有的pm2.5城市（官网：http://www.thinkpage.cn/）
			String[] arr = {"北京","上海","天津","重庆",
					"哈尔滨","齐齐哈尔","牡丹江","佳木斯","绥化","黑河","大兴安岭","伊春","大庆","七台河","鸡西","鹤岗","双鸭山",
					"长春","吉林","四平","通化","白城","辽源","松原","白山","沈阳","大连","鞍山","抚顺","本溪","丹东","锦州",
					"营口","阜新","辽阳","铁岭","朝阳","盘锦","葫芦岛","呼和浩特","包头","乌海","通辽","赤峰","鄂尔多斯",
					"呼伦贝尔","乌兰浩特","石家庄","保定","张家口","承德","唐山","廊坊","沧州","衡水",
					"邢台","邯郸","秦皇岛","郑州","安阳","新乡","许昌","平顶山","信阳","南阳","开封","洛阳","商丘","焦作","鹤壁","濮阳",
					"周口","漯河","驻马店","三门峡","太原","大同","阳泉","晋中","长治","晋城","临汾","运城","朔州","忻州","吕梁",
					"济南","青岛","淄博","德州","烟台","潍坊","济宁","泰安","临沂","菏泽","滨州","东营","威海","枣庄","日照","莱芜","聊城",
					"南京","无锡","镇江","苏州","南通","扬州","盐城","徐州","淮安","连云港","常州","泰州","宿迁","杭州","湖州","嘉兴","宁波",
					"绍兴","台州","温州","丽水","金华","衢州","舟山","福州","厦门","宁德","莆田","泉州","漳州","龙岩","三明","南平","南昌","九江",
					"上饶","抚州","宜春","吉安","赣州","景德镇","萍乡","新余","鹰潭","合肥","蚌埠","芜湖","淮南","马鞍山","安庆","宿州","阜阳","亳州",
					"黄山","滁州","淮北","铜陵","宣城","六安","池州","武汉","襄阳","鄂州","孝感","黄冈","黄石","咸宁","荆州","宜昌","恩施","十堰",
					"随州","荆门","长沙","湘潭","株洲","衡阳","郴州","常德","益阳","娄底","邵阳","岳阳","张家界","怀化","永州",
					"广州","韶关","惠州","梅州","汕头","深圳","珠海","佛山","肇庆","湛江","江门","河源","清远","云浮","潮州","东莞","中山","阳江","揭阳",
					"茂名","汕尾","南宁","崇左","柳州","来宾","桂林","梧州","贺州","贵港","玉林","百色","钦州","河池","北海","防城港","海口","三亚","贵阳",
					"遵义","安顺","铜仁","毕节","昆明","大理","红河","曲靖","保山","文山","玉溪","楚雄","普洱","昭通","临沧",
					"怒江","丽江","德宏","成都","攀枝花","自贡","绵阳","南充","达州","遂宁","广安","巴中","泸州","宜宾","内江","资阳","乐山",
					"眉山","凉山","雅安","甘孜","阿坝","德阳","广元","拉萨","日喀则","山南","林芝","昌都","那曲","阿里","西安","咸阳","延安","榆林","渭南","商洛",
					"安康","汉中","宝鸡","铜川","银川","石嘴山","吴忠","固原","中卫","兰州","定西","平凉","庆阳","武威","金昌","张掖","酒泉","天水",
					"临夏","甘南","白银","嘉峪关","西宁","海东","黄南","海南","果洛","玉树","海西","海北","乌鲁木齐","克拉玛依","石河子","昌吉","吐鲁番",
					"阿克苏","喀什","塔城","哈密","和田","阿勒泰"};
			//判断是否在城市列表里
			if(Arrays.asList(arr).contains(cityName1)){
//			      测试汉字是否转码
//				try {
//					cityName=URLEncoder.encode(cityName, "UTF-8");
//				} catch (UnsupportedEncodingException e1) {
//					e1.printStackTrace();
//				}
				airNowObject = weatherInfoService.getAirNowInfo(cityName);
				if (airNowObject.has("results")){
					JSONArray results = airNowObject.getJSONArray("results");
					JSONObject result = results.getJSONObject(0);
					JSONObject air = result.getJSONObject("air");
					JSONObject city = air.getJSONObject("city");
					String pm25 = city.getString("pm25");
					String aqi = city.getString("aqi");
					String quality = city.getString("quality");
					resultDto.put("pm25", pm25);
					resultDto.put("aqi", aqi);
					resultDto.put("quality", quality);
				} else {
					responseDto.addErrorInfo(ErrorConstant.HD_GET_PM_FAIL, airNowObject.getString("status"));
					logger.error("************" + airNowObject.getString("status"));
				}
				responseDto.pushResultDto(resultDto);
			}else{
				responseDto.addErrorCode(ErrorConstant.HD_GET_PM_FAIL);
				logger.error("*********************该城市没有pm2.5*************************");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			responseDto.addErrorInfo(ErrorConstant.HD_GET_PM_FAIL, e.getMessage());
			e.printStackTrace();
		}
		
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		RedisClient redisClient = new RedisClient();
//		redisClient.setAddress("127.0.0.1");
//		redisClient.setPort(6379);
//		
//		WeatherInfoService weatherInfoService = new WeatherInfoService();
//		weatherInfoService.setKey("QN2ZD5D6JN");
//		weatherInfoService.setRedisClient(redisClient);
//		
//		AirNowAction airNowAction = new AirNowAction();
//		airNowAction.setWeatherInfoService(weatherInfoService);
//		
//		airNowAction.getAirNowByCityName();
//		airNowAction.getAirNowByCityName();
//	}

}
